package com.leo;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/**
 * @ClassName TableViewCustomCellDemo
 * @Description 五种数据基本类型：CheckBoxTableCell, ChoiceBoxTableCell, ComboBoxTableCell, ProgressBarTableCell, TextFieldTableCell
 * @Author wangss
 * @date 2020.06.13 23:13
 * @Version 1.0
 */
public class TableViewFiveCellTypeDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        StudentPro s1 = new StudentPro("小a", 10, 0.5d, false);
        StudentPro s2 = new StudentPro("小b", 11, 0.65d, true);
        StudentPro s3 = new StudentPro("小c", 11, 0.74d, true);
        StudentPro s4 = new StudentPro("小d", 9, 0.98d, true);

        ObservableList<StudentPro> list = FXCollections.observableArrayList();
        list.addAll(s1, s2, s3, s4);

        TableView<StudentPro> tableView = new TableView<>(list);

        tableView.setEditable(true);

        TableColumn<StudentPro, String> name = new TableColumn<>("姓名");
        name.setCellValueFactory(new PropertyValueFactory<>("name"));
        name.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter<String>() {
            @Override
            public String toString(String object) {
                return object + "自定义";
            }

            @Override
            public String fromString(String string) {
                return null;
            }
        }));

        TableColumn<StudentPro, Integer> age = new TableColumn<>("年龄");
        age.setCellValueFactory(new PropertyValueFactory<>("age"));
        age.setCellFactory(ComboBoxTableCell.forTableColumn(1, 2, 3, 4));

        TableColumn<StudentPro, Double> score = new TableColumn<>("成绩");
        score.setCellValueFactory(new PropertyValueFactory<>("score"));
        score.setCellFactory(ProgressBarTableCell.forTableColumn());

        TableColumn<StudentPro, Boolean> flag = new TableColumn<>("是否及格");
        flag.setCellValueFactory(new PropertyValueFactory<>("flag"));
        flag.setCellFactory(CheckBoxTableCell.forTableColumn(flag));

        tableView.getColumns().addAll(name, age, score, flag);

        AnchorPane.setTopAnchor(tableView, 50d);
        AnchorPane.setLeftAnchor(tableView, 50d);
        anchorPane.getChildren().addAll(tableView);

        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.setTitle("TableView");
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}
